import React,{useEffect, useState} from 'react'

function Hooks02(props) {
    const initialState = 2;

    const [timer ,setTimer] = useState(initialState);
    useEffect(()=>{
        // console.log("useEffect被执行")
        const timerOut = setTimeout(()=>{
        // console.log("setInterval被执行")
            setTimer(timer+1);
        },1000)
        return ()=>{
            clearInterval(timerOut)
            
        }
    },[timer]);

    return (<>
    <button onClick={() => setTimer(timer *2) }>x2</button>
    <button onClick={() => setTimer(timer -1) }>-1</button>
    <button onClick={() => setTimer(initialState) }>重置</button>

    <div>第一个函数组件：{timer}</div>
    
    
    </>
    )
}
export default Hooks02;